﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;  //  for Vector2
using Microsoft.Xna.Framework.Graphics;   //   for Texture2D
using Microsoft.Xna.Framework.Content;

namespace zombieArena
{
    //abstract class Weapon
    class Weapon
    {
        public float rateOfFire { get; set; }
        public float bulletSpeed { get; set; }
        public int ammo { get; set; }
        public int maxAmmo { get; set; }
        public string name { get; set; }
        public Texture2D texture;
        public int soundIndex;

        public Weapon(Texture2D t)
        {
            texture = t;
            soundIndex = 0;
        }

        public virtual void shoot(Vector2 position, Vector2 direction, Map map)
        {
            map.bulletList.Add(new Bullet(position, direction * bulletSpeed, texture));
            ammo--;
        }
        //public bool outOfAmmo();






        // prototype stuff
        public Weapon(Texture2D t, int type)
        {
            texture = t;

            switch (type)
            {
                case 1:
                    prototypeinit1(); break;
                case 2:
                    prototypeinit2(); break;
                case 3:
                    prototypeinit3(); break;
                case 4:
                    prototypeinit4(); break;
                case 5:
                    prototypeinit5(); break;
                default: 
                    prototypeinit1(); break;
            }
        }
        public void prototypes(Vector2 position, Vector2 direction, Map map, int type, Texture2D boom)
        {
            switch (type)
            {
                case 1: 
                    prototype1(position, direction, map); break;
                case 2:
                    prototype2(position, direction, map); break;
                case 3:
                    prototype3(position, direction, map); break;
                case 4:
                    prototype4(position, direction, map); break;
                case 5:
                    prototype5(position, direction, map, boom); break;

                default: shoot(position, direction, map); break;
            }
        }

        public void prototypeinit1()
        {
            rateOfFire = 100f;
            bulletSpeed = 30f;
            name = "Super Shotgun";
        }

        public void prototype1(Vector2 position, Vector2 direction, Map map)
        {
            int shotgunPellets = 100;

            Random r = new Random();
            Vector2 recoil = new Vector2(0, 0);

            for (int i = 0; i < shotgunPellets; i++)
            {
                float x = ((float)r.Next(-5000, 5000) / 10000);
                float y = ((float)r.Next(-5000, 5000) / 10000);
                recoil = new Vector2(direction.X + x, direction.Y + y);

                map.bulletList.Add(new Bullet(position, recoil * bulletSpeed, texture));
            }        
        }

        public void prototypeinit2()
        {
            rateOfFire = 1;
            bulletSpeed = 10f;
            name = "I'M FIRING MAH LASERZZZ";
        }
        public void prototype2(Vector2 position, Vector2 direction, Map map)
        {
            int numberPewPew = 20;

            if (direction.X == 0)
            {
                for (int i = 0; i < numberPewPew; i++)
                {
                    map.bulletList.Add(new Bullet(new Vector2(position.X - (numberPewPew / 2 * 8) + i * 10, position.Y), direction * bulletSpeed, texture));
                }
            }
            else
            {
                for (int i = 0; i < numberPewPew; i++)
                {
                    map.bulletList.Add(new Bullet(new Vector2(position.X, position.Y - (numberPewPew / 2 * 8) + i * 10), direction * bulletSpeed, texture));
                }
            }
        }

        public void prototypeinit3()
        {
            rateOfFire = 150f;
            bulletSpeed = 15f;
            name = "Spread Gun";
        }

        public void prototype3(Vector2 position, Vector2 direction, Map map)
        {
            Random r = new Random();
            Vector2 recoil = new Vector2(0, 0);
            map.bulletList.Add(new Bullet(position, direction * bulletSpeed, texture));
            if (direction.X == 0)
            {
                recoil = new Vector2(direction.X + 0.5f, direction.Y);
                recoil.Normalize();
                map.bulletList.Add(new Bullet(position, recoil * bulletSpeed, texture));
                recoil = new Vector2(direction.X - 0.5f, direction.Y);
                recoil.Normalize();
                map.bulletList.Add(new Bullet(position, recoil * bulletSpeed, texture));
            }
            else
            {
                recoil = new Vector2(direction.X, direction.Y + 0.5f);
                recoil.Normalize();
                map.bulletList.Add(new Bullet(position, recoil * bulletSpeed, texture));
                recoil = new Vector2(direction.X, direction.Y - 0.5f);
                recoil.Normalize();
                map.bulletList.Add(new Bullet(position, recoil * bulletSpeed, texture));
            }
        }

        public void prototypeinit4()
        {
            rateOfFire = 500f;
            bulletSpeed = 30f;
            name = "Rifle";
        }

        public void prototype4(Vector2 position, Vector2 direction, Map map)
        {
            map.bulletList.Add(new Bullet(position, direction * bulletSpeed, texture, 1, 30f, 0f, 10000f, 0f, new Vector2(0f,0f)));
        }
        public void prototype4idle(Vector2 position, Vector2 direction, Map map, SpriteBatch spriteBatch)
        {
            Vector2 laserPointer = position;

            int laserCount = 0;
            while (laserCount < 1000)
            {
                if (laserPointer.X < 0 || laserPointer.X > 1600 || laserPointer.Y < 0 || laserPointer.Y > 1200)
                {
                    return;
                }

                int i = (int)laserPointer.X / map.cellSize;
                int j = (int)laserPointer.Y / map.cellSize;
                int zombieCount = 0;
                while (zombieCount < map.enemyList[i, j].Count)
                {
                    Enemy enemy = map.enemyList[i, j][zombieCount];

                    // distance between bullets and zombies
                    double distance = Math.Sqrt(Math.Pow(laserPointer.X - enemy.position.X, 2.0) + Math.Pow(laserPointer.Y - enemy.position.Y, 2.0));
                    if (distance < 30)
                    {
                        Vector2 enemyscreenpos = enemy.position - new Vector2(map.viewPort.X, map.viewPort.Y);
                        //spriteBatch.Draw(enemy.charTexture, enemyscreenpos, new Rectangle(40 * enemy.currentSprite, 56 * enemy.spriteRow, enemy.width, enemy.height), Color.Red);
                        enemy.draw(enemyscreenpos, spriteBatch, Color.Red);
                        return;
                    }
                    zombieCount++;
                }
                Vector2 laserscreenpos = laserPointer - new Vector2(map.viewPort.X, map.viewPort.Y);

                laserCount++;
                spriteBatch.Draw(texture, laserscreenpos, null, Color.White, 0f, new Vector2(0,0),0.2f,SpriteEffects.None,0f);
                laserPointer = laserPointer + direction;
            }

            
        }


        public void prototypeinit5()
        {
            rateOfFire = 500f;
            bulletSpeed = 30f;
            name = "Rocket Launcher";
        }
        public void prototype5(Vector2 position, Vector2 direction, Map map, Texture2D boom)
        {
            map.bulletList.Add(new Bullet(position, direction * bulletSpeed, texture, 2, 50f, boom, 125f, 10000f, 9000f, new Vector2(0f, 0f), new Vector2(0f, 0f)));
        }

    }
}
